package com.lv.phuotbacnam.ui.navigation;

import android.app.Activity;
import android.app.Fragment;
import android.os.Handler;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.toolbox.ScreenUtils;
import com.lv.phuotbacnam.ui.fragment.LeftMenuFragment;
import com.lv.phuotbacnam.ui.fragment.RightMenuFragment;

/**
 * Sliding menu wrapper
 */
public class NavigationMenu {

	/** Activity to build the navigation menu for */
	private Activity activity;

	/** Menu slider */
	private SlidingMenu menu;

	/** Menu mode */
	private Mode mode;

	public NavigationMenu(Activity activity) {
		this(activity, Mode.LEFT);
	}

	public NavigationMenu(Activity activity, Mode mode) {
		this.activity = activity;
		this.mode = mode;
	}

	public void initialize() {
		menu = new SlidingMenu(activity);
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		menu.setShadowWidthRes(R.dimen.menu_shadow_width);
		menu.setShadowWidthRes(R.dimen.menu_shadow_width);
		menu.setShadowDrawable(R.drawable.shadow);
		menu.setSecondaryShadowDrawable(R.drawable.shadowright);
		menu.setBehindOffset((int) (ScreenUtils.getWidth(activity) * 0.125));
		menu.setFadeDegree(0.35f);
		menu.attachToActivity(activity, SlidingMenu.SLIDING_WINDOW);
		setMode(mode);

	}

	public void setMenu(int menuFrameId, Fragment fragment) {
		activity.getFragmentManager().beginTransaction()
				.replace(menuFrameId, fragment).commit();
	}

	public void setLeftMenu(Fragment fragment) {
		setMenu(R.id.left_menu_frame, fragment);
	}

	public void setRightMenu(Fragment fragment) {
		setMenu(R.id.right_menu_frame, fragment);
	}

	public void setMode(Mode mode) {
		this.mode = mode;
		switch (mode) {
		case LEFT:
			menu.setMode(SlidingMenu.LEFT);
			menu.setShadowDrawable(R.drawable.shadow);
			menu.setMenu(R.layout.menu_left_frame);
			setLeftMenu(new LeftMenuFragment());
			break;
		case RIGHT:
			menu.setMode(SlidingMenu.RIGHT);
			menu.setShadowDrawable(R.drawable.shadowright);
			menu.setSecondaryMenu(R.layout.menu_right_frame);
			setRightMenu(new RightMenuFragment());
			break;
		case LEFT_RIGHT:
			menu.setMode(SlidingMenu.LEFT_RIGHT);
			menu.setSecondaryShadowDrawable(R.drawable.shadowright);
			menu.setShadowDrawable(R.drawable.shadow);
			menu.setMenu(R.layout.menu_left_frame);
			menu.setSecondaryMenu(R.layout.menu_right_frame);
			setLeftMenu(new LeftMenuFragment());
			setRightMenu(new RightMenuFragment());
			break;
		}
	}

	/**
	 * Show the left menu
	 */
	public void showLeftMenu() {
		if (mode != Mode.RIGHT) {
			menu.showMenu();
		}
	}

	/**
	 * Show the right menu
	 */
	public void showRightMenu() {
		if (mode != Mode.LEFT) {
			menu.showSecondaryMenu();
		}
	}

	public void hideMenu() {
		responsiveUiAction(new Runnable() {
			@Override
			public void run() {
				menu.showContent();
			}
		});
	}

	private void responsiveUiAction(Runnable runnable) {
		// For responsive UI
		Handler handler = new Handler();
		handler.postDelayed(runnable, 50);
	}

	/**
	 * Menu mode
	 */
	/**
	 * Menu mode
	 */
	public static enum Mode {
		LEFT, RIGHT, LEFT_RIGHT
	}
}
